/**
 * 
 */
package template.page.user;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.SortedSet;

import org.apache.click.control.ActionButton;
import org.apache.click.control.Column;
import org.apache.click.control.Label;
import org.apache.click.control.Table;
import org.apache.click.dataprovider.DataProvider;
import org.apache.click.util.Bindable;

import template.page.BorderPage;
import cvcg.model.AttendingPlayer;
import cvcg.model.Game;
import cvcg.model.GameSession;
import cvcg.util.Top5ResultDecorator;
import cvcg.util.VoteResultDecorator;

/**
 * @author jaracje
 * 
 */
@SuppressWarnings({ "rawtypes", "serial" })
public class GameSessionDetailClosed extends BorderPage {
	public String title = "Session Results";

	private Table attendingPlayerTable = new Table("attendingPlayerTable");
	private Label gameSessionDateLabel = new Label("gameSessionDate");
	private Table voteTable = new Table("voteTable");
	private Table top5Table = new Table("top5Table");	
	private Table playedGamesTable = new Table("playedGamesTable");
	private ActionButton backButton = new ActionButton("backButton","back");
	private GameSession currentGameSession;

	@Bindable
	protected Long id;

	public GameSessionDetailClosed() {
		
		addControl(gameSessionDateLabel);
		addControl(attendingPlayerTable);
		addControl(voteTable);
		addControl(top5Table);
		addControl(playedGamesTable);
		addControl(backButton);
		backButton.setListener(this, "onBackClick");
		
		attendingPlayerTable.setClass(Table.CLASS_ITS);
		attendingPlayerTable.setWidth("100%");
		attendingPlayerTable.setSortable(false);

		Column dateColumn = new Column("joinDate");
		dateColumn.setFormat("{0,date,dd/MM/yyyy HH:mm}");
		attendingPlayerTable.addColumn(dateColumn);
		attendingPlayerTable.addColumn(new Column("playerName"));
		attendingPlayerTable.addColumn(new Column("ownedGameCount"));
		attendingPlayerTable.addColumn(new Column("knownGameCount"));
		attendingPlayerTable.setSortedColumn("joinDate");

		attendingPlayerTable.setDataProvider(new DataProvider() 
		{	        
			@SuppressWarnings("unchecked")
			public List getData() 
	        {
	        	List attendingPlayers = new ArrayList();
	        	if (currentGameSession!=null) 
	        	{
	        		attendingPlayers.addAll(currentGameSession.getAttendingPlayers());
	        	}
	            return attendingPlayers;	        
	        }
	    });

	}

	@Override
	public void onGet() {
		if (id != null) 
		{
			currentGameSession = gameSessionService.getGameSessionById(id);
			if (currentGameSession!=null) 
			{
				SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");
				gameSessionDateLabel.setLabel(sdf.format(currentGameSession.getDate()));
				buildVoteTable();
				buildTop5Table();
				buildPlayedGamesTable();
			}
		}
	}
	
	public void buildVoteTable() {
		voteTable.setClass(Table.CLASS_ITS);
		voteTable.setShowBanner(false);
		voteTable.setSortable(false);

	
		SortedSet<AttendingPlayer> attendingPlayers = currentGameSession
				.getAttendingPlayers();
		String playerName;
		voteTable.addColumn(new Column("gameName"));
		Column playerCol;
		for (AttendingPlayer attendingPlayer : attendingPlayers) {
			playerName = attendingPlayer.getPlayerName();
			playerCol = new Column(playerName);
			playerCol.setTextAlign("center");
			playerCol.setDecorator(new VoteResultDecorator(playerName));
			voteTable.addColumn(playerCol);
		}
		Column votes = new Column("votes");
		votes.setTextAlign("center");
		voteTable.addColumn(votes);
		voteTable.setSortedColumn("gameName");

		voteTable.setDataProvider(new DataProvider() {

			public Collection getData() {
				return gameSessionService.getVoteResultForGameSession(id);

			}
		});
	}
	
	public void buildTop5Table() {
		top5Table.setClass(Table.CLASS_ITS);
		top5Table.setShowBanner(false);
		top5Table.setSortable(false);

		
		SortedSet<AttendingPlayer> attendingPlayers = currentGameSession
				.getAttendingPlayers();
		String playerName;
		top5Table.addColumn(new Column("gameName"));
		Column playerCol;
		for (AttendingPlayer attendingPlayer : attendingPlayers) {
			playerName = attendingPlayer.getPlayerName();
			playerCol = new Column(playerName);
			playerCol.setTextAlign("center");
			playerCol.setDecorator(new Top5ResultDecorator(playerName));
			top5Table.addColumn(playerCol);
		}
		Column points = new Column("points");
		points.setTextAlign("center");
		top5Table.addColumn(points);
		top5Table.setSortedColumn("points");
		top5Table.setSortedAscending(false);

		top5Table.setDataProvider(new DataProvider() {

			public Collection getData() {
				return gameSessionService.getTop5ResultForGameSession(id);
			}
		});
	}

	public void buildPlayedGamesTable() 
	{
		playedGamesTable.setClass(Table.CLASS_ITS);
		playedGamesTable.setShowBanner(false);
		playedGamesTable.setSortable(false);
		
		playedGamesTable.addColumn(new Column("name"));
		playedGamesTable.setSortedColumn("name");		

		playedGamesTable.setDataProvider(new DataProvider() 
		{
			public Collection getData() 
			{							
				GameSession gameSession = gameSessionService.getGameSessionById(id);
				List<Game> gameList = new ArrayList<Game>();
				if (gameSession != null) 
				{
					gameList.addAll(gameSession.getPlayedGames());
				}
				return gameList;
			}
		});
	}

	/**
     * Handle the back click event.
     */    
    public boolean onBackClick() 
    {
    	setRedirect(HomePage.class);        
        return true;
    }
}
